import type {
  SysUserDtlVo,
  SysUserLoginInfoVo,
  SysUserPageQo,
  SysUserPasswordQo,
  SysUserQo,
  SysUserUpdateLoginQo,
  SysUserVo,
} from '#/system/user'
import { http } from '@/utils/request/index'

/**
 * 获取当前登录用户信息
 * @returns Promise<SysUserLoginInfoVo>
 */
export function getByLoginUserInfoApi() {
  return http.request<SysUserLoginInfoVo>('get', '/admin-api/system/sysUser/sysUserInfo')
}

/**
 * 获取用户分页列表
 * @param params 查询参数
 * @returns 用户列表
 */
export async function getSysUserPageListApi(params: SysUserPageQo) {
  const res = await http.request<PageResult<SysUserVo>>('get', '/admin-api/system/sysUser/sysUserPage', {
    params: {
      ...params,
      roleIdList: params.roleIdList?.join(','),
      postIdList: params.postIdList?.join(','),
    },
  })
  res.data.rows.forEach((item) => {
    if (item.deptNameList != null && item.deptNameList.length > 0) {
      item._deptNameList = item.deptNameList.split(',')
    }
    if (item.postNameList != null && item.postNameList.length > 0) {
      item._postNameList = item.postNameList.split(',')
    }
    if (item.roleNameList != null && item.roleNameList.length > 0) {
      item._roleNameList = item.roleNameList.split(',')
    }
  })
  return res
}

/**
 * 获取用户列表
 * @param params 查询参数
 * @returns 用户列表
 */
export function getSysUserListApi(params?: SysUserPageQo) {
  return http.request<SysUserVo[]>('get', '/admin-api/system/sysUser/sysUserList', {
    params,
  })
}

/**
 * 获取用户详情
 * @param id 用户id
 * @returns 用户详情
 */
export function getSysUserDetailsApi(id?: string) {
  return http.request<SysUserDtlVo>('get', `/admin-api/system/sysUser/sysUserDetails/${id}`)
}

/**
 * 新增用户
 * @param data 用户信息
 * @returns 用户id
 */
export function createSysUserApi(data: SysUserQo) {
  return http.request<string>('post', '/admin-api/system/sysUser/sysUserCreate', {
    data,
  })
}

/**
 * 修改用户
 * @param data 用户信息
 * @returns 用户id
 */
export function updateSysUserApi(data: SysUserQo) {
  return http.request<string>('post', '/admin-api/system/sysUser/sysUserUpdate', {
    data,
  })
}

/**
 * 修改当前登陆用户信息
 * @param data 用户信息
 * @returns 用户id
 */
export function updateSysUserByLoginApi(data: SysUserUpdateLoginQo) {
  return http.request<string>('post', '/admin-api/system/sysUser/sysUserUpdateLogin', {
    data,
  })
}

/**
 * 修改用户密码
 * @param data 用户信息
 * @returns 用户id
 */
export function updateSysUserPasswordApi(data: SysUserPasswordQo) {
  return http.request<string>('post', '/admin-api/system/sysUser/sysUserUpdatePassword', {
    data,
  })
}

/**
 * 删除用户
 * @param idList 用户id列表
 * @returns 用户id
 */
export function deleteSysUserApi(idList: string[]) {
  return http.request<string>('post', '/admin-api/system/sysUser/sysUserRemove', {
    params: {
      idList: idList.join(','),
    },
  })
}

/**
 * 启用用户
 * @param idList 用户id列表
 * @returns 用户id
 */
export function enableSysUserApi(idList: string[]) {
  return http.request<string>('post', '/admin-api/system/sysUser/sysUserEnable', {
    params: {
      idList: idList.join(','),
    },
  })
}

/**
 * 禁用用户
 * @param idList 用户id列表
 * @returns 用户id
 */
export function disableSysUserApi(idList: string[]) {
  return http.request<string>('post', '/admin-api/system/sysUser/sysUserDisable', {
    params: {
      idList: idList.join(','),
    },
  })
}
